<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <title>编辑优惠券活动</title>
    <script th:src="@{/static/assets/js/jquery.min.js}"></script>
    <script src="http://libs.useso.com/js/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
    <link rel="stylesheet" th:href="@{/static/component/pear/css/pear.css}" />

<!--    日期插件css-->
    <link rel="stylesheet" type="text/css" th:href="@{/static/pear-admin/css/bootstrap.min.css}" />
    <link rel="stylesheet" th:href="@{/static/pear-admin/css/dcalendar.picker.css}"/>

    <style>
        .layui-form-label{
            width: 110px;
        }
    </style>

</head>
<body>
<form class="layui-form" th:action="@{/pear/edit/couponsActivity}" method="post" id="fm" autocomplete="off">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <input type="hidden" name="id" th:value="${couponsActivity.getId()}">
                    <label class="layui-form-label">优惠券名称</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.couponsName}" type="text" name="couponsName" lay-verify="title" autocomplete="off" placeholder="请输入优惠券名称" class="layui-input">
                    </div>

                    <label class="layui-form-label">优惠券数量</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.getCouponsCount()}" type="text" name="couponsCount" lay-verify="title" autocomplete="off" placeholder="请输入优惠券数量" class="layui-input">
                    </div>

                    <label class="layui-form-label">优惠券金额</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.amount}" type="text" name="amount" lay-verify="title" autocomplete="off" placeholder="请输入优惠券金额" class="layui-input">
                    </div>

                    <label class="layui-form-label">每人限领</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.limitCount}" type="text" name="limitCount" lay-verify="title" autocomplete="off" placeholder="每人限制领取多少张" class="layui-input">
                    </div>


                    <label class="layui-form-label">使用门槛</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.minPoint}" type="text" name="minPoint" lay-verify="title" autocomplete="off" placeholder="使用门槛,也就是满多少元才可以使用" class="layui-input">
                    </div>

                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-block">
                        <input id="mydatepicker1" type="text" name="startTime" lay-verify="title" autocomplete="off" placeholder="优惠券开始时间" class="layui-input">
                    </div>

                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-block">
                        <input  id="mydatepicker2" type="text" name="endTime" lay-verify="title" autocomplete="off" placeholder="优惠券结束时间" class="layui-input">
                    </div>

                    <label class="layui-form-label">优惠券备注</label>
                    <div class="layui-input-block">
                        <input th:value="${couponsActivity.note}" type="text" name="note" lay-verify="title" autocomplete="off" placeholder="优惠券备注" class="layui-input">
                    </div>

                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                修改
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script th:src="@{/static/component/layui/layui.js}"></script>
<script th:src="@{/static/component/pear/pear.js}"></script>

<!--日期插件js-->
<script type="text/javascript" th:src="@{/static/pear-admin/dcalendar.picker.js}"></script>

<script>

<!--    格式化日期插件-->
    $('#mydatepicker1').dcalendarpicker({
        format:'yyyy-mm-dd'
    });

    $('#mydatepicker2').dcalendarpicker({
        format:'yyyy-mm-dd'
    });


    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let jq = $.noConflict();
        // 获取<meta>标签中封装的_csrf信息 ,否则会请求403
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var headers = {"X-CSRF-TOKEN": token}

        form.on('submit(user-save)', function (data) {
            layer.confirm('您确定要修改该优惠券活动吗?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);

                let loading = layer.load();
                $.ajax({
                    url:'/pear/edit/couponsActivity',
                    data: jq('#fm').serialize(),
                    dataType:'json',
                    //一定不能写contentType: xxx json ,不然响应结果就会输出到页面上，success回调函数就接收不到
                    type:'post',
                    headers: headers,
                    success: function (result) {
                        layer.close(loading);
                        if(result.success){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    },
                    error:function () {
                        layer.close(loading);
                        layer.msg("修改优惠券活动失败",{icon:2,time:1000});
                    }
                })
            })
            return false;
        });



    })
</script>
<script>
</script>
</body>
</html>